import cgi
import webapp2
import hashlib
import random
import string

MAIN_PAGE_HTML = """\
<html><head><title>OH MY HASH!</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
function randstr() {
var randomstring = Math.random().toString(36).slice(-15);
document.getElementById('salt').value = randomstring;
}

function newdat() {
var today = new Date();
document.getElementById('salt').value = today;
}

function newhou() {
var today = new Date();
document.getElementById('salt').value = today.getTime();
}

function newusa() {
var agent = navigator.userAgent;
document.getElementById('salt').value = agent;
}
</script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
  <body>
  <h3>Oh my Hash!</h3>
  <tt>The older smartass brother of JesusCrypt! - Developed by MauroEldritch @ <a href="http://eldritch.com.ar">Eldritch Software</a></tt><BR>
  <!-- I'm becoming older, you may soon expect KissMyHash or some other random cheap genitalia-related joke name for my next hasher... -->
  <BR>  
    
    <form id="hasher" name="hasher" action="/hash" method="post">
      <div><tt><b>String to hash:</b></tt><BR><textarea name="content" rows="3" cols="60"></textarea></div><BR>
      <div><tt><b>Salt (optional):</b></tt><BR><textarea id="salt" name="salt" rows="3" cols="60"></textarea></div><BR>
      <tt><b>Salt presets:</b> </tt><BR><BR>
      <input type="button" value="Current Date" onclick="newdat()">
      <input type="button" value="Current Hour" onclick="newhou()">
      <input type="button" value="User Agent" onclick="newusa()">
      <input type="button" value="Random String" onclick="randstr()">
      
      </BR><BR>      
      <div><select id="algos" name="algos">
      <option value="md5">md5</option>
      <option value="sha1">sha1</option>
            <option value="sha224">sha224</option>
                  <option value="sha256">sha256</option>
                        <option value="sha384">sha384</option>
                              <option value="sha512">sha512</option>
      </select><input type="submit" value="Hash"></div>
    </form>
  </body>
</html>
"""


class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.write(MAIN_PAGE_HTML)


class Hash(webapp2.RequestHandler):

    def post(self):
    	salt = self.request.get('salt')
    	if (salt == ""):
    		rawvalue = self.request.get('content')
    	else:
    		rawvalue = self.request.get('content') + salt
    		
    	algos = self.request.get('algos')
    	if (algos == "md5"):
    		hashedvalue = hashlib.md5(rawvalue).hexdigest()
    	elif (algos == "sha1"):
    		hashedvalue = hashlib.sha1(rawvalue).hexdigest()
    	elif (algos == "sha224"):
    		hashedvalue = hashlib.sha224(rawvalue).hexdigest()
    	elif (algos == "sha256"):
    		hashedvalue = hashlib.sha256(rawvalue).hexdigest()
    	elif (algos == "sha384"):
    		hashedvalue = hashlib.sha384(rawvalue).hexdigest()
    	elif (algos == "sha512"):
    		hashedvalue = hashlib.sha512(rawvalue).hexdigest()
    	
        self.response.write('<html><head><script type="text/javascript" src="jquery.js"></script><script>function gbak() { window.location = "http://ohmyhash.appspot.com";}</script><script>$(document).ready(function(){$(".slidingDiv").hide();$(".show_hide").show();$(\'.show_hide\').click(function(){$(".slidingDiv").slideToggle();});});</script><link rel="stylesheet" type="text/css" href="style.css"></head><body><h3>Oh my Hash!</h3><tt>The older smartass brother of JesusCrypt! - Developed by MauroEldritch @ <a href="http://eldritch.com.ar">Eldritch Software</a></tt><BR><BR><tt><b>Hashed value:</b></tt><BR><BR><textarea name="uno" rows="3" cols="60">')
        self.response.write(cgi.escape(hashedvalue))
        self.response.write('</textarea><BR><BR><a href="#" class="show_hide"><tt>Show/Hide original input</tt></a><BR><div class="slidingDiv"><BR><tt><b>Salt:</b></tt><BR><BR><textarea name="dos" rows="3" cols="60">')
        self.response.write(cgi.escape(salt))
        self.response.write('</textarea><BR><BR><a href="#" class="show_hide"></a></div><BR> <a href="#" class="show_hide"></a><div class="slidingDiv"><tt><b>Final String: </b></tt><BR><BR><textarea name="tres" rows="3" cols="60">')
        self.response.write(cgi.escape(rawvalue))
        self.response.write('</textarea><BR><BR><a href="#" class="show_hide"><tt>Hide</tt></a></div><BR><input type="button" value="Hash another string" onclick="gbak()"></body></html>')



app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/hash', Hash),
], debug=True)